#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstring>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <ctime>

char op[4]={'+','-','*','/'};

//计算请求
class Request
{
public:
    Request(int id,int n1,int n2,int op)
        :_id(id),_n1(n1),_n2(n2),_op(op)
    {}
public:
    int _id;
    int _n1;
    int _n2;
    char _op;
};

//计算结果
class Response
{
public:
    Response(std::string request, int result)
        :_request(request),_result(result),_errno(0)
    {}
public:
    std::string _request;
    int _result;
    int _errno;
};

//执行计算的函数
void* handler(void *args)
{
    Request *rq=static_cast<Request*>(args);
    std::string req="thread "+std::to_string(rq->_id)+" : "+std::to_string(rq->_n1)\
                    +rq->_op+std::to_string(rq->_n2);
    int res,flag=0;
    switch (rq->_op) 
    {
    case '+':
        res = rq->_n1 + rq->_n2;
        break;
    case '-':
        res = rq->_n1 - rq->_n2;
        break;
    case '*':
        res = rq->_n1 * rq->_n2;
        break;
    case '/':
        if (rq->_n2 != 0) 
        {
            res = rq->_n1 / rq->_n2;
        } 
        else 
        {
            flag=1;
            std::cerr << "Error: Division by zero!" << std::endl; 
        }
        break;
    }

    Response *rsp=new Response(req,res);
    if(flag==1) rsp->_errno=1;
    std::cout<<"thread is working"<<std::endl;
    //std::cout<<rq->_id<<" thread result : "<<rq->_n1<<"+"<<rq->_n2<<"="<<rsp->_result<<std::endl;
    //usleep(1000);
    return rsp;
}

//我们使用多线程模拟一个简单的多线程加减乘除计算过程
void test2()
{
    //设置随机数种子
    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    //模拟10次加减乘除计算过程
    std::vector<pthread_t> threads;
    std::vector<Response*> responses; // 用于存储每个线程的返回值

    for(int i=1;i<=10;i++)
    {
        pthread_t tid;
        int n1=rand()%10;
        int n2=rand()%10;
        Request *rq=new Request(i,n1,n2,op[n1%4]);
        pthread_create(&tid,nullptr,handler,rq);
        threads.push_back(tid); // 将线程句柄存储起来
    }

    for(auto& thread:threads)
    {
        void *ret;
        pthread_join(thread,&ret);
        Response *rsp = static_cast<Response *>(ret);
        std::cout<<rsp->_result<<std::endl;
        std::cout << "rsp->result: " << rsp->_result <<std::endl;    
        responses.push_back(rsp); // 将 Response 对象存储起来
    }

    // 输出每个线程的计算结果
    for (auto rsp : responses)
    {
        if(rsp->_errno==1)  
        {
            std::cout <<rsp->_request << " result: " << "division by zero error" << std::endl;
        }
        else 
        {
            std::cout <<rsp->_request << " result: " << rsp->_result << std::endl;
        }
        delete rsp; // 释放每个 Response 对象的内存
    }
}

int g_val=100;

//线程的处理函数
void *handler1(void*)
{
    pthread_t tid = pthread_self();  // 获取当前线程的 POSIX 线程 ID
    g_val++;
    std::cout << "Thread ID: " << tid <<" "<< g_val << std::endl;
    return nullptr;
}

//线程函数的使用
void test1()
{
    //我们创建十个线程，并且打印各自的信息
    std::vector<pthread_t> threads;
    
    //创建十个线程
    for(int i=1;i<=10;i++)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,handler1,nullptr);
        threads.push_back(tid);
    }

    //进行线程等待释放资源
    for(int i=0;i<10;i++)
    {
        pthread_t tid=threads[i];
        pthread_join(tid,nullptr);
    }
}   

//使用线程函数进行简单计算

int main()
{
    //test1();
    test2();
    return 0;
}